<?php


namespace App\Dao;


use App\Model\TaskPush;
use Hyperf\Database\Model\Builder;

class TaskPushDao extends AbstractDao
{

    public function where(array $where): \Hyperf\Database\Model\Builder
    {
        $builder = TaskPush::query();

        foreach ($where as $field => $value) {
            switch ($field) {
                case 'received_cooperative_ids':
                    $builder->whereIn('received_cooperative_id', $value);
                    break;
                case 'cooperative_ids':
                    $builder->whereIn('cooperative_id', $value);
                    break;
                case 'keywords':
                    $this->whereKeywords($builder, $value);
                    break;
                default:
                    $builder->where($field, $value);
            }
        }
        return $builder;
    }

    public function whereKeywords(Builder $builder, $value)
    {
        $builder->whereHas('user', function ($query) use ($value) {
            $query->where('phone', $value);
            $query->orWhere('id', $value);
        })->orWhereHas('task', function ($query) use ($value) {
            $query->where('title', 'like', '%' . $value . '%');
            $query->orWhere('id', $value);
        });
    }
}